package online.westonline.com.westonline.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.base.BaseActivity;
import online.westonline.com.westonline.ui.fragment.message.ReadFragment;
import online.westonline.com.westonline.ui.fragment.message.UnReadFragment;
import online.westonline.com.westonline.utils.UiUtlis;

public class MessageActivity extends BaseActivity {

    @BindView(R.id.tab_message_layout)
    TabLayout tabMessageLayout;
    @BindView(R.id.view_message_pager)
    ViewPager viewMessagePager;
    @BindView(R.id.tv_topbar_title)
    TextView tvTopbarTitle;
    @BindView(R.id.tv_topbar_title_center)
    TextView tvTopbarTitleCenter;
    @BindView(R.id.img_toolbar_menu)
    ImageView imgToolbarMenu;
    @BindView(R.id.tv_topbar_menu)
    TextView tvTopbarMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    private List<String> titles;
    private UnReadFragment unReadFragment;
    private ReadFragment readFragment;
    private List<Fragment> fragments = new ArrayList<>();
    private MyViewPager myViewPager;

    @Override
    protected void initview() {
        toolbar.setTitle("消息列表");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        viewMessagePager.setAdapter(myViewPager);
        tabMessageLayout.setupWithViewPager(viewMessagePager);
    }

    @SuppressLint("ResourceType")
    @Override
    protected void initdata() {
        //请求所有消息列表(放fragment里)
        myViewPager = new MyViewPager(getSupportFragmentManager());
        titles = UiUtlis.getViewPagerTitle(R.array.Message);
        unReadFragment = new UnReadFragment();
        readFragment = new ReadFragment();
        fragments.add(unReadFragment);
        fragments.add(readFragment);
    }

    @Override
    protected int setlayout() {
        return R.layout.activity_message;
    }

    @Override
    protected void onResume() {
        super.onResume();
        unReadFragment.updateData();
        readFragment.updateData();
    }


    /***
     * viewpager
     */
    class MyViewPager extends FragmentPagerAdapter {

        public MyViewPager(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }

    //加载菜单

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.top_message_menu,menu);
        return true;
    }

    //删除

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.action_message_delete:

                break;
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }
}
